#-*-coding:utf-8-*-
# import json
# import socket


import sys
import os

# import time
# import datetime
# import threading

G_base_path = os.path.dirname(os.path.dirname(__file__.replace("\\", "/")))
G_com_path = G_base_path + "/base/com_lib"
G_cgtw_path = G_base_path + "/base"
G_plugin_lib_path = G_base_path + '/timeaxis_plugin/lib'
G_cgtw_pyside = G_base_path +'/lib/pyside'

if not G_base_path in sys.path:
    sys.path.append(G_base_path)
if not G_com_path in sys.path:
    sys.path.append(G_com_path)
if not G_cgtw_path in sys.path:
    sys.path.append(G_cgtw_path)
if not G_plugin_lib_path in sys.path:
    sys.path.append(G_plugin_lib_path)
if not G_cgtw_pyside in sys.path:
    sys.path.append(G_cgtw_pyside)
# from PySide2 import QtGui,QtCore,QtWidgets
print G_cgtw_pyside
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from com_message_box import *
# from com_work_log import *
# from com_function import *
import cgtw2

class GetTaskInfo(QWidget):
    def __init__(self):
        super(GetTaskInfo,self).__init__()
        #全局变量
        self.t_tw = cgtw2.tw()
        self.t_module = self.t_tw.client.get_module()
        self.t_id_list = self.t_tw.client.get_id()
        self.t_database = self.t_tw.client.get_database()

        self.setWindowTitle('选中的任务信息')
        self.setMinimumWidth(900)
        self.main_layout=QVBoxLayout()
        self.main_layout.setSpacing(10)
        self.show_label=QLabel('选中的任务名称与任务id')

        self.text_brow=QTextBrowser()
        self.main_layout.addWidget(self.show_label)
        self.main_layout.addWidget(self.text_brow)

        self.setLayout(self.main_layout)

    def getAllDatabase(self):
        '''
        该函数调用cgtw的api获取当前所有激活的项目名称(database)
        返回项目数据库的列表
        :return:[u'proj_pltest', u'proj_zkzyj', u'proj_xxss', u'proj_hbl', u'proj_xmcc', u'proj_pqcs', u'proj_aaa',
        u'proj_qjxtest', u'proj_cy_backup', u'proj_ts_backup', u'proj_csr_backup', u'proj_eyyyqn_backup']

        '''
        all_projects = []
        t_id_list = self.t_tw.info.get_id('public', 'project',
                                          [["project.status", "!=", "Lost"], "and", ["project.status", "!=", "Close"]])
        t_data = self.t_tw.info.get('public', 'project', t_id_list, ['project.database','project.full_name','project.frame_rate','project.entity_name','resolution'])

        # 过滤掉模板项目“pltest”
        for i in t_data:
            if i != 'proj_pltest':
                all_projects.append(i['project.database'])
        self.active_projects = all_projects
        print(all_projects)

        return all_projects


    def getTaskInfo(self):
        if self.t_id_list:
            self.text_brow.clear()
            text=''
            for i in self.t_id_list:
                shot_name=' '
                link_id=''
                if self.t_module=='shot':
                    shot_name = self.t_tw.task.get(self.t_database, self.t_module, [i], ['shot.shot'])[0][
                        'shot.shot']
                    link_id = self.t_tw.info.get_id(self.t_database, self.t_module,
                                                    [["shot.shot", "=", shot_name]])[0]
                    entity_name=self.t_tw.info.get('public','project',)

                # elif self.t_module=='asset':
                #     shot_name = self.t_tw.task.get(self.t_database, self.t_module, [i], ['asset.asset_name'])[0][
                #         'asset.asset_name']
                #     link_id = self.t_tw.info.get_id(self.t_database, self.t_module,
                #                                     [["asset.asset_name", "=", shot_name]])[0]
                task_id=self.t_tw.task.get(self.t_database, self.t_module, [i], ['task.id'])[0][
                    'task.id']

                cg_id=self.t_database+','+self.t_module+','+task_id
                text+='镜头号:{}  任务id:{} link_id:{} cg_id:{}\n\n'.format(shot_name,task_id,link_id,cg_id)
            self.text_brow.setText(text)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    g=GetTaskInfo()
    # g.getTaskInfo()
    g.getAllDatabase()
    g.show()
    sys.exit(app.exec_())